/**
 * @param {number[]} jobDifficulty
 * @param {number} d
 * @return {number}
 */
var minDifficulty = function (jobDifficulty, d) {
  const len = jobDifficulty.length
  if (len < d) return -1
  let dp = new Array(d+1).fill(0).map(()=>new Array(len).fill(0x3f3f3f3f))
  let max = 0
  for(let i=0;i<len;i++){
    max = Math.max(max,jobDifficulty[i])
    dp[0][i]=max
  }

  for(let i=1;i<d;i++){
    for(let j=i;j<len;j++){
      max = 0
      for(let k=j;k>=i;k--){
        max = Math.max(max,jobDifficulty[k])
        dp[i][j]=Math.min(dp[i][j],max+dp[i-1][k-1])
      }
    }
  }

  return dp[d-1][len-1]
};

minDifficulty( [7,1,7,1,7,1],3)